<template>
  <div ref="chart"  :style="chartStyle">
  </div>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'customcar',
  props: {
    config: {
      required: true,
    },
    chartStyle: {
      require: false,
      type: Object,
      default: () => ({
        height: '100%',
        position: 'absolute',
        width: '100%',
        top: '0px',
        bottom: '0px',
        left: '0px',
      }),
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {

  },
  mounted() {
    this.renderChart();
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    renderChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart, this.config.theme);
      }
      const option = this.makeOptions(this.config, this.chartData);
      this.chart.clear();
      this.chart.setOption(option);
      setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    makeOptions() {
      const imgSrc = '';
      const sportsIcon = {
        a: '',
        b: '',
        c: '',
        d: '',
        e: '',
      };
      return {
        // backgroundColor: '#2b2a38',
        graphic: {
          elements: [{
            type: 'image',
            z: 6,
            style: {
              image: imgSrc,
              width: 100,
              shadowBlur: 0,
              shadowColor: '#000',
              shadowOffsetX: '1',
              shadowOffsetY: '1',
            },
            left: 'center',
            top: '40%',
            position: [100, 100],
          }],
        },
        title: [{
          text: '',
          textAlign: 'center',
          x: '34%',
          y: '48%',
          textStyle: {
            color: '#8c949a',
            fontSize: 20,
            fontWeight: 'normal',
          },
        }],
        series: [
          {
            name: 'title',
            type: 'pie',
            radius: ['35%', '90%'],
            center: ['50%', '50%'],
            color: [
              new echarts.graphic.LinearGradient(0.3, 1, 0.8, 0.1, [{
                offset: 0,
                color: '#09b094',
              }, {
                offset: 1,
                color: '#015648',
              },
              ]),
              new echarts.graphic.LinearGradient(0, 0.5, 1, 0.7, [{
                offset: 0,
                color: '#633248',
              }, {
                offset: 1,
                color: '#40001d',
              },
              ]),
              new echarts.graphic.LinearGradient(0.1, 0, 0.1, 1, [{
                offset: 0,
                color: '#f09913',
              }, {
                offset: 1,
                color: '#6b4100',
              },
              ]),
              new echarts.graphic.LinearGradient(1, 0.5, 0, 0.7, [{
                offset: 0,
                color: '#c2352c',
              }, {
                offset: 1,
                color: '#6f0700',
              },
              ]),
              new echarts.graphic.LinearGradient(1, 0.7, 0.6, 0.1, [{
                offset: 0,
                color: '#1e77b1',
              }, {
                offset: 1,
                color: '#003354',
              },
              ]),

            ],
            label: {
              normal: {
                formatter(params) {
                  let str = '';
                  switch (params.name) {
                    case 'Monday':
                      str = `${'{a|}\n{nameStyle|Monday }{rate|\n'}${params.value}%}`;
                      break;
                    case 'Tuesday':
                      str = `${'{b|}\n{nameStyle|Tuesday }{rate|\n'}${params.value}%}`;
                      break;
                    case 'Wednesday':
                      str = `${'{c|}\n{nameStyle|Wednesday }{rate|\n'}${params.value}%}`;
                      break;
                    case 'Thursday':
                      str = `${'{d|}\n{nameStyle|Thursday }{rate|\n'}${params.value}%}`;
                      break;
                    case 'Friday':
                      str = `${'{e|}\n{nameStyle|Friday }{rate|\n'}${params.value}%}`;
                      break;
                    default:
                  }
                  return str;
                },
                position: 'inside',
                rich: {
                  a: {
                    width: 50,
                    height: 50,
                    lineHeight: 50,
                    shadowBlur: 0,
                    shadowColor: '#2b2a38',
                    shadowOffsetX: '1',
                    shadowOffsetY: '1',
                    backgroundColor: {
                      image: sportsIcon.e,
                    },
                  },
                  b: {
                    width: 50,
                    height: 50,
                    lineHeight: 50,
                    shadowBlur: 0,
                    shadowColor: '#2b2a38',
                    shadowOffsetX: '1',
                    shadowOffsetY: '1',
                    backgroundColor: {
                      image: sportsIcon.d,
                    },
                  },
                  c: {
                    width: 50,
                    height: 50,
                    lineHeight: 50,
                    shadowBlur: 0,
                    shadowColor: '#2b2a38',
                    shadowOffsetX: '1',
                    shadowOffsetY: '1',
                    backgroundColor: {
                      image: sportsIcon.c,
                    },
                  },
                  d: {
                    width: 50,
                    height: 50,
                    lineHeight: 50,
                    shadowBlur: 0,
                    shadowColor: '#2b2a38',
                    shadowOffsetX: '1',
                    shadowOffsetY: '1',
                    backgroundColor: {
                      image: sportsIcon.b,
                    },
                  },
                  e: {
                    width: 50,
                    height: 50,
                    lineHeight: 50,
                    shadowBlur: 0,
                    shadowColor: '#2b2a38',
                    shadowOffsetX: '1',
                    shadowOffsetY: '1',
                    backgroundColor: {
                      image: sportsIcon.a,

                    },
                  },
                  nameStyle: {
                    fontSize: 16,
                    color: '#FFF',
                    textBorderColor: 'rgba(0, 0, 0, 0)',
                    textShadowColor: '#2b2a38',
                    textShadowBlur: '0',
                    textShadowOffsetX: 1,
                    textShadowOffsetY: 1,
                  },
                  rate: {
                    fontSize: 20,
                    lineHeight: 30,
                    color: '#FFF',
                    textBorderColor: 'rgba(0, 0, 0, 0)',
                    textShadowColor: '#2b2a38',
                    textShadowBlur: '0',
                    textShadowOffsetX: 1,
                    textShadowOffsetY: 1,
                  },
                },
              },
            },
            labelLine: {
              normal: {
                smooth: true,
                length: 10,
                lineStyle: {
                  width: 1.5,
                },
              },
            },
            itemStyle: {
              normal: {
                borderColor: '#2b2a38',
                borderWidth: 3,
              },
            },
            data: [{
              value: 20,
              name: 'Monday',
            },
            {
              value: 20,
              name: 'Tuesday',
            },
            {
              value: 20,
              name: 'Wednesday',
            },
            {
              value: 20,
              name: 'Thursday',
            },
            {
              value: 20,
              name: 'Friday',
            },
            ],
          },
          {
            type: 'pie',
            radius: ['35%', '90%'],
            center: ['50%', '50%'],
            z: 0,
            data: [{
              hoverOffset: 1,
              value: 100,
              itemStyle: {
                color: 'rgba(251, 200, 79, 1)',
                shadowBlur: 20,
                shadowColor: '#241210',
              },
              label: {
                show: false,
              },
              labelLine: {
                normal: {
                  smooth: true,
                  lineStyle: {
                    width: 0,
                  },
                },
              },
              hoverAnimation: false,
            }],
          },
          {
            type: 'pie',
            radius: ['25%', '0%'],
            center: ['50%', '50%'],
            z: 0,
            data: [{
              hoverOffset: 1,
              value: 100,
              itemStyle: {
                color: '#3d3c4a',
              },
              label: {
                show: false,
              },
              labelLine: {
                normal: {
                  smooth: true,
                  lineStyle: {
                    width: 0,
                  },
                },
              },
              hoverAnimation: false,
            }],
          },
        ],
      };
    },
  },
  watch: {
    config: {
      deep: true,
      handler() {
        if (this.config.needResize) {
          this.config.needResize = false;
          this.handleResize();
        }
      },
    },
  },
};
</script>

<style scoped lang="stylus">

</style>
